package jwtx

import (
	"context"
	"encoding/json"
	"github.com/golang-jwt/jwt/v4"
)

// GetToken 参数一 密钥，参数二时间戳，参数三 过期时间 参数四userid等自定义信息
func GetToken(secretKey string, iat int64, accessExpire int64, uid int64) (string, error) {
	claims := make(jwt.MapClaims)
	claims["expires"] = accessExpire
	claims["iat"] = iat
	claims["uid"] = uid

	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims

	return token.SignedString([]byte(secretKey))

}

// GetUidByToken Context里面存在从token解析出来的uid
func GetUidByToken(ctx context.Context) (int64, error) {
	uid, err := ctx.Value("uid").(json.Number).Int64()
	return int64(uid), err
}
